<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Paylog
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Paylog_Controller_Calender extends Paylog_Abstract_PageController
{
	protected $balanceHand = null;
	protected $month = null;
	protected $bookId = null;

	protected $calenderFirst = null;
	protected $calenderLast  = null;
	protected $monthFirst = null;
	protected $monthLast  = null;

	public function __construct()
	{
		parent::__construct();
		$this->balanceHand = new Paylog_Object_BalanceHandler;
		$this->month  = Paylog::get('month', date('Y-m'));
		$this->bookId = Paylog::get('book_id');
		$this->_detectSpan();

		$this->output['month']   =& $this->month;
		$this->output['book_id'] =& $this->bookId;
	}

	public function main()
	{
		$this->_validateAccess();
		$this->_default();
	}

	protected function _validateAccess()
	{
		if ( !$this->bookId )
		{
			Paylog::location('book');
		}

		if ( !preg_match('/^[1-2][0-9][0-9][0-9]-(?:0[1-9]|1[0-2])$/', $this->month) )
		{
			Paylog::redirect("Invalid month.");
		}

		$bookHand = new Paylog_Object_BookHandler;

		if ( !$bookHand->isBookExists($this->bookId) )
		{
			Paylog::redirect("Book deos not exists.");
		}
	}

	protected function _default()
	{
		$this->_outputDays();

		$this->output['previous'] = $this->_getMonthInfo($this->monthFirstDay - 86400);
		$this->output['current']  = $this->_getMonthInfo($this->monthFirstDay);
		$this->output['next']     = $this->_getMonthInfo($this->monthLastDay + 86400);

		$this->output['month_format'] = Paylog::msg("%B");
		$this->output['date_format']  = Paylog::msg("%B %Y");

		$this->menu['start'] = date('Y-m-d', $this->monthFirstDay);
		$this->menu['end']   = date('Y-m-d', $this->monthLastDay);
		$this->menu['book_id'] = $this->bookId;
		$this->menu['month']   = $this->month;

		$this->_view();
	}

	protected function _getMonthInfo($timeStamp)
	{
		return array(
			'time'  => $timeStamp,
			'year'  => date('Y', $timeStamp),
			'month' => date('m', $timeStamp),
			'date'  => date('Y-m', $timeStamp),
		);
	}

	protected function _detectSpan()
	{
		$this->monthFirstDay = strtotime(date($this->month.'-01'));
		$this->monthLastDay  = strtotime(date('Y-m-t', $this->monthFirstDay));

		$lastMonth = date('w', $this->monthFirstDay);
		$nextMonth = 7 - date('w', $this->monthLastDay);
		
		$this->calenderFirstDay = $this->monthFirstDay - ( $lastMonth * 86400 );
		$this->calenderLastDay  = $this->monthLastDay + ( $nextMonth * 86400 );
	}

	protected function _outputDays()
	{
		$days = array();

		$time = $this->calenderFirstDay;

		while ( $this->calenderLastDay > $time )
		{
			$date = date('Y-m-d', $time);
			$days[$date] =  array(
				'month'   => date('n', $time),
				'day'     => date('j', $time),
				'weekday' => date('w', $time),
				'date'    => date('Y-m-d', $time),
				'time'    => $time,
			);
			$time += 86400;
		}

		$start = $this->calenderFirstDay;
		$end   = $this->calenderLastDay;

		$flows = array(
			'out' => Paylog_Object_Balance::FLOW_OUT,
			'in'  => Paylog_Object_Balance::FLOW_IN,
		);

		foreach ( $flows as $name => $value )
		{
			$totals = $this->balanceHand->loadSumMonthly($start, $end, $value, $this->bookId);

			foreach ( $totals as &$total )
			{
				$date = date('Y-m-d', $total['date']);
				$days[$date][$name] = $total['sum'];
			}
		}

		$this->output['days'] = $days;
	}
}

?>
